/**
 * 一键拨号
 */
define(function(require, exports, module) {
    var $ = require("jquery");
    var util = require("util");
    var ui = require("ui");
    var tmpl = require("tmpl");
    var dialog = require("dialog");
    require("pagination");
    require("permission");
    var api_prefix = G_CONFIG["api_prefix"] + "tools/basis-function-plugin/"; //api前缀

    var speedDial = {
        list:[],
        pagerInited:false,
        pageIndex:1,
        pageSize:10,
        init: function() {
            this.pageIndex = 1;
            this.getList();
            this.bindEvent();
        },
        initPager:function(total){
            var _this = this;
            $('#pagination').pagination(total,{
                num_edge_entries: 1, //边缘页数
                num_display_entries: 4, //主体页数
                callback: function (pageIndex, jq) {
                    _this.pageIndex = pageIndex;
                    _this.getList(pageIndex);
                },
                items_per_page: this.pageSize //每页显示1项
            });
            this.pagerInited = true;
        },
        bindEvent: function() {
            var _this = this;
            $("#btn_add").off().on("click",function(e){
                e.preventDefault();
                _this.showAddDynamic("",{});
            });
            $("#dial_grid").on("click", ".js-edit", function(e) {
                e.preventDefault();
                var oid = $(e.currentTarget).closest("td").attr("id") || "";
                if (oid) {
                    _this.showUpdateDynamic(oid);
                }
            });
            $("#dial_grid").on("click", ".js-del", function(e) {
                e.preventDefault();
                var oid = $(e.currentTarget).closest("td").attr("id") || "";
                if (oid) {
                    _this.del(oid);
                }
            });
            $("#dial_grid").on("mouseenter","tr",function(){
                $(this).find(".js-oper-btn").show();
            });
            $("#dial_grid").on("mouseleave","tr",function(){
                $(this).find(".js-oper-btn").hide();
            });
        },
        showUpdateDynamic: function(oid) {
            var dynamic;
            $.each(this.list, function(i, item) {
                if (item.pk_speed_dialId == oid) {
                    dynamic = item;
                    return false;
                }
            });
            if (!dynamic) {
                ui.showErrTip("未找到对象");
                return;
            }
            this.showAddDynamic(dynamic.pk_speed_dialId, dynamic);
        },
        //新增,编辑记录
        showAddDynamic: function(oid, dynamic) {
            var title = oid ? "编辑记录" : "新增记录";
            var html = tmpl("add_dial_tmpl", dynamic);
            var _this = this;
            var d = dialog({
                id:"save_dial_dialog",
                title: title,
                content: html,
                width:720,
                height:510,
                onshow: function() {
                    require.async(["uploader"], function() {
                        $('#fileupload').fileupload({
                            dataType: 'json',
                            url: G_CONFIG["api_prefix"] + "common/file/upload-image",
                            add:function(e,data){
                                data.submit();
                                $("#img_wrap").append('<div class="upload-process">上传中...</div>');
                            },
                            done: function(e, data) {
                                var res = data.result;
                                if (res.retCode == "0") {
                                    var img_url = res.data.path;
                                    $("#img_wrap").html('<img src="' + img_url + '" />');
                                    $("#picurl").val(img_url);
                                    $("#fileupload").nextAll(".js-err").html("");
                                } else {
                                    $("#fileupload").nextAll(".js-err").html("上传失败:" + res.errMsg);
                                    $("#picurl").val("");
                                }
                            }
                        });
                    });
                     //初始化编辑器
                    var editor = new UE.ui.Editor({
                        zIndex: 9999,
                        toolbars:window.UEDITOR_CONFIG.toolbarStyles["default"]
                    });
                    editor.ready(function(){
                        if(dynamic.introduction){
                            editor.setContent(dynamic.introduction);
                        }
                    });
                    editor.render("editor");
                },
                ok: function() {
                    var phone_number = ui.checkField("phone_number", true, "电话号码不能为空");
                    var welcomes = ui.checkField("welcomes", true, "欢迎语不能为空");
                    var picurl = ui.checkField("picurl", true, "请上传顶部图片");
                    var content = ui.checkEditor("editor",true, "楼盘介绍不能为空");
                    if (phone_number === false || welcomes === false || picurl === false || content === false) {
                        return false;
                    }
                    _this.save(oid,phone_number,welcomes,picurl,content);
                    return false;
                },
                cancel: function() {
                }
            });
            d.showModal();
        },
        getList:function(pageIndex){
            if(typeof pageIndex == "undefined"){
                pageIndex = 1;
                this.pagerInited = false;
            }
            var url = api_prefix + "get-pk-speed-dial-list";
            var data = {
                pageIndex:pageIndex,
                pageSize:this.pageSize
            };
            util.myAjax({
                url: url,
                type: "GET",
                cache: false,
                data: data,
                context: this,
                success: function(json) {
                    if (json.retCode == 0) {
                        this.list = json.data.list || []; //列表
                        this.initTable(this.list);
                        if(!this.pagerInited){
                            this.initPager(json.data.total);
                        }
                    } else {
                        ui.showErrTip(json.errMsg);
                    }
                },
                complete:function(){
                    ui.removeLoading();
                }
            });
            ui.loading();
        },
        initTable:function(list){
            var html = tmpl('dial_row_tmpl', {list: list});
            $("#dial_grid").html(html);
        },
        del:function(oid){
            var _this = this;
            util.confirm("您确定要删除吗？", {
                ok: function() {
                    var url = api_prefix + "del-pk-speed-dial";
                    var data = {
                        pk_speed_dialId: oid
                    };
                    util.myAjax({
                        url: url,
                        type: "POST",
                        context: _this,
                        data: data,
                        success: function(json) {
                            if (json.retCode == 0) {
                                _this.getList();
                            } else {
                                ui.showErrTip(json.errMsg);
                            }
                        }
                    });
                }
            });
        },
        //添加、编辑
        save: function(oid,phone_number,welcomes,picurl,content) {
            if(this.isSubmit){
                ui.showErrTip("请勿重复提交");
                return;
            }
            var url = api_prefix + "write-pk-speed-dial";
            var data = {
                phone_number: phone_number,
                welcomes: welcomes,
                picurl: picurl,
                introduction: content
            };
            if(oid){ //更新
                data.pk_speed_dialId = oid;
            }
            util.myAjax({
                url: url,
                type: "POST",
                context: this,
                data: data,
                success: function(json) {
                    this.isSubmit = false;
                    if (json.retCode == 0) {
                        dialog.get("save_dial_dialog").remove();
                        ui.showSucTip("操作成功");
                        this.getList();
                    } else {
                        ui.showErrTip(json.errMsg);
                    }
                },
                complete:function(){
                    this.isSubmit = false;
                }
            });
            this.isSubmit = true;
        }
    };
    module.exports = speedDial;
});